package com.zhitong.money.controller;

import com.github.pagehelper.PageInfo;
import com.zhitong.money.entity.AjaxResponse;
import com.zhitong.money.entity.Salary;
import com.zhitong.money.exception.RepeatException;
import com.zhitong.money.services.api.ISalaryService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

@CrossOrigin
@Slf4j
@RestController
public class SalaryContro {

    @Autowired
    private ISalaryService service;

    @GetMapping("/salarys")
    public AjaxResponse getAllSalary() throws Exception{
        List<Salary> allSalary = service.findAllSalary();
        return AjaxResponse.success(allSalary);
    }

    @GetMapping("/salary/{id}")
    public AjaxResponse getSalaryById(@PathVariable String id) throws Exception{
        Salary salaryById = service.findSalaryById(id);
        return AjaxResponse.success(salaryById);
    }

    @PostMapping("/salarys")
    public AjaxResponse getSalaryByMap(@RequestBody Map<String,Object> map) throws Exception{

        PageInfo salaryByMap = service.findSalaryByPage(map);
        return AjaxResponse.success(salaryByMap);

    }

    @RequiresPermissions({"addSalary"})
    @PostMapping("/salary")
    public AjaxResponse addSalary(@RequestBody Salary salary) throws Exception{
        service.addSalary(salary);
        return AjaxResponse.success("添加该工资成功!");
    }

    @RequiresPermissions({"updateSalary"})
    @PutMapping("/salary")
    public AjaxResponse updateSalary(@RequestBody Salary salary) throws Exception{
        service.updateSalary(salary);
        return AjaxResponse.success("该工资修改成功!");
    }

    @RequiresPermissions({"delSalary"})
    @DeleteMapping("/salary/{id}")
    public AjaxResponse delSalary(@PathVariable("id")String id) throws Exception{
        service.delSalary(id);
        return AjaxResponse.success("删除该工资成功!");
    }

    @RequiresPermissions({"delSalary"})
    @PostMapping("/delsalarys")
    public AjaxResponse delSalaryByIds(@RequestBody List<String> ids) throws Exception{
        service.delSalaryByIds(ids);
        return AjaxResponse.success("删除工资成功!");
    }

    @RequiresPermissions({"export"})
    @PostMapping("/exportSalarys")
    public  void exportCheck(@RequestBody Map<String,Object> map, HttpServletResponse response) {
        try {
            service.exportSalarys(map,response);
        } catch (Exception e) {
            log.error(e.getMessage());
        }
    }

}
